package com.shenmazong.thread;

/**
 * @program: server-java-demo
 * @description: SynchronizedTest6
 * @author: 亮子说编程
 * @create: 2020-10-24 23:06
 **/

class DoCookEgg implements Runnable {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+":开始做面包了。。。");
        while (true) {
            System.out.println(Thread.currentThread().getName()+"我就缺面粉了。。。");
            synchronized (SynchronizedTest6.egg) {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                synchronized (SynchronizedTest6.flour) {
                    System.out.println(Thread.currentThread().getName()+"开始做面包。。。");
                }
            }
        }
    }
}

class DoCookFlour implements Runnable {

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+":开始做面包了。。。");
        while (true) {
            System.out.println(Thread.currentThread().getName()+"我就缺鸡蛋了。。。");
            synchronized (SynchronizedTest6.flour) {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                synchronized (SynchronizedTest6.egg) {
                    System.out.println(Thread.currentThread().getName()+"开始做面包。。。");
                }
            }
        }
    }
}
public class SynchronizedTest6 {

    public static String egg = "鸡蛋";
    public static String flour = "面粉";

    public static void main(String[] args) {
        Thread threadEgg = new Thread(new DoCookEgg());
        Thread threadFlour = new Thread(new DoCookFlour());

        threadEgg.start();
        threadFlour.start();
    }

}
